package com.ktgj.customer.controller;

import java.util.List;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ktgj.common.log.annotation.SignatureValidation;
import com.ktgj.customer.domain.vo.CommonAddressesVo;
import com.ktgj.customer.domain.vo.ProvincialVo;
import com.ktgj.customer.service.ILoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ktgj.common.log.annotation.Log;
import com.ktgj.common.log.enums.BusinessType;
import com.ktgj.common.security.annotation.PreAuthorize;
import com.ktgj.customer.domain.CommonAddresses;
import com.ktgj.customer.service.ICommonAddressesService;
import com.ktgj.common.core.web.controller.BaseController;
import com.ktgj.common.core.web.domain.AjaxResult;
import com.ktgj.common.core.utils.poi.ExcelUtil;
import com.ktgj.common.core.web.page.TableDataInfo;

/**
 * 常用地址Controller
 * 
 * @author ruoyi
 * @date 2021-09-01
 */
@ControllerAdvice
@RestController
@RequestMapping("/addresses")
public class CommonAddressesController extends BaseController
{
    @Autowired
    private ICommonAddressesService commonAddressesService;
    @Autowired
    private ILoginService loginService;

    /**
     * 查询常用地址列表
     */
    //@PreAuthorize(hasPermi = "customer:addresses:list")
    @GetMapping("/list")
    public TableDataInfo list(CommonAddresses commonAddresses)
    {
        startPage();
        List<CommonAddresses> list = commonAddressesService.selectCommonAddressesList(commonAddresses);
        return getDataTable(list);
    }

    /**
     * 导出常用地址列表
     */
    //@PreAuthorize(hasPermi = "customer:addresses:export")
    @Log(title = "常用地址", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CommonAddresses commonAddresses) throws IOException
    {
        List<CommonAddresses> list = commonAddressesService.selectCommonAddressesList(commonAddresses);
        ExcelUtil<CommonAddresses> util = new ExcelUtil<CommonAddresses>(CommonAddresses.class);
        util.exportExcel(response, list, "常用地址数据");
    }

    /**
     * 获取常用地址详细信息
     */
    //@PreAuthorize(hasPermi = "customer:addresses:query")
    @GetMapping(value = "/{commonAddresserId}")
    public AjaxResult getInfo(@PathVariable("commonAddresserId") Long commonAddresserId)
    {
        return AjaxResult.success(commonAddressesService.selectCommonAddressesById(commonAddresserId));
    }

    /**
     * 新增常用地址
     */
    //@PreAuthorize(hasPermi = "customer:addresses:add")
    @Log(title = "常用地址", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CommonAddresses commonAddresses)
    {
        return toAjax(commonAddressesService.insertCommonAddresses(commonAddresses));
    }

    /**
     * 修改常用地址
     */
    //@PreAuthorize(hasPermi = "customer:addresses:edit")
    @Log(title = "常用地址", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CommonAddresses commonAddresses)
    {
        return toAjax(commonAddressesService.updateCommonAddresses(commonAddresses));
    }

    /**
     * 删除常用地址
     */
    //@PreAuthorize(hasPermi = "customer:addresses:remove")
    @Log(title = "常用地址", businessType = BusinessType.DELETE)
	@DeleteMapping("/{commonAddresserIds}")
    public AjaxResult remove(@PathVariable Long[] commonAddresserIds)
    {
        return toAjax(commonAddressesService.deleteCommonAddressesByIds(commonAddresserIds));
    }

    /**
     * 我的-新增常用地址
     * */
    //@SignatureValidation
    @PostMapping("/editCommonlyUsedAddress")
    public AjaxResult editCommonlyUsedAddress(HttpServletRequest request, @RequestBody CommonAddressesVo commonAddressesVo){
        if (commonAddressesVo.getDetailedAddress().length() >= 5){
            Long customerId;
            try {
                customerId = loginService.getLoginCustomer(request).getCustomerId();
            } catch (Exception e){
                return AjaxResult.error(e.getMessage());
            }
            commonAddressesVo.setMemberId(customerId);
            return AjaxResult.success(commonAddressesService.saveCommonAddressesByCustomerId(commonAddressesVo));
        }
        return AjaxResult.error("详细地址不能少于5个汉字，请重新输入");
    }

    /**
     * 获取城市信息
     * */
    @PostMapping("/areaList")
    public AjaxResult areaList(){
        List<ProvincialVo> provincialVoList = commonAddressesService.areaList();
        if (provincialVoList.size() > 0){
            return AjaxResult.success(provincialVoList);
        }
        else {
            return AjaxResult.error("城市信息获取失败");
        }
    }

    /**
     * 我的-修改常用地址
     * */
    //@SignatureValidation
    @PostMapping("/changeAddress")
    public AjaxResult changeAddress(HttpServletRequest request, @RequestBody CommonAddresses commonAddresses){
        Long customerId;
        try {
            customerId = loginService.getLoginCustomer(request).getCustomerId();
        } catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
        commonAddresses.setMemberId(customerId);

        int byCustomerId;
        try {
            byCustomerId = commonAddressesService.updateCommonAddressesByCustomerId(commonAddresses);
        } catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }

        if (byCustomerId > 0){
            return AjaxResult.success("修改成功");
        }
        else {
            return AjaxResult.error("修改失败");
        }
    }

    /**
     * 删除常用地址
     * */
    @PostMapping("/deleteAddress")
    public AjaxResult deleteAddress(@RequestBody Map<String, Object> map){
        Long commonAddresserId = Long.valueOf(map.get("commonAddresserId").toString());
        int byId = commonAddressesService.deleteCommonAddressesById(commonAddresserId);
        if (byId > 0){
            return toAjax(byId);
        }
        else {
            return AjaxResult.error("删除失败");
        }
    }

    /**
     * 查询待编辑的常用地址
     * */
    @PostMapping("/addressToBeModified")
    public AjaxResult addressToBeModified(@RequestBody Map<String, Object> map){
        Long commonAddresserId = Long.valueOf(map.get("commonAddresserId").toString());
        CommonAddresses commonAddresses = commonAddressesService.addressToBeModified(commonAddresserId);
        if (commonAddresses != null){
            return AjaxResult.success(commonAddresses);
        }
        else {
            return AjaxResult.error("未查询到数据");
        }
    }

    /**
     * 后端——查询常用地址
     * */
    @PostMapping("/backSelectAddresses")
    public AjaxResult backSelectCommonAddresses(@RequestBody Map<String, Object> map){
        if (map.get("memberId") != null && !map.get("memberId").equals("")){
            Long memberId = Long.valueOf(map.get("memberId").toString());

            List<CommonAddresses> commonAddressesList;
            try {
                commonAddressesList = commonAddressesService.backSelectCommonAddresses(memberId);
            } catch (Exception e){
                return AjaxResult.error(e.getMessage());
            }
            if (commonAddressesList != null && commonAddressesList.size() > 0){
                return AjaxResult.success(commonAddressesList);
            }
            else {
                return AjaxResult.error("未查到数据");
            }
        }
        else {
            return AjaxResult.error("缺少必要参数");
        }
    }

    /**
     * 后端——查询待修改的常用地址
     * */
    @PostMapping("/backAddressesById")
    public AjaxResult backSelectCommonAddressesById(@RequestBody Map<String, Object> map){
        if (map.get("commonAddresserId") != null && !map.get("commonAddresserId").equals("")){
            Long commonAddresserId = Long.valueOf(map.get("commonAddresserId").toString());
            CommonAddresses commonAddresses;
            try {
                commonAddresses = commonAddressesService.backSelectCommonAddressesById(commonAddresserId);
            } catch (Exception e){
                return AjaxResult.error(e.getMessage());
            }
            if (commonAddresses != null){
                return AjaxResult.success(commonAddresses);
            }
            else {
                return AjaxResult.error("未查到数据");
            }
        }
        else {
            return AjaxResult.error("缺少必要参数");
        }
    }

    /**
     * 后端——新增常用地址
     * */
    @PostMapping("/backInsertAddresses")
    public AjaxResult backInsertCommonAddresses(@RequestBody CommonAddresses commonAddresses){
        if (commonAddresses != null){
            if(commonAddresses.getMemberId()==null||commonAddresses.getMemberId()==0l){
                return AjaxResult.error("缺少必要参数");
            }
            int i;
            try {

                i = commonAddressesService.backInsertCommonAddresses(commonAddresses);
            } catch (Exception e){
                return AjaxResult.error(e.getMessage());
            }
            if (i > 0){
                return AjaxResult.success("新增成功");
            }
            else {
                return AjaxResult.error("新增失败");
            }
        }
        else {
            return AjaxResult.error("缺少必要参数");
        }
    }
    /**
     * 后端——修改常用地址
     * */
    @PostMapping("/backUpdateAddresses")
    public AjaxResult backUpdateCommonAddresses(@RequestBody CommonAddresses commonAddresses){
        if (commonAddresses != null){
            if(commonAddresses.getMemberId()==null||commonAddresses.getMemberId()==0l){
                return AjaxResult.error("缺少必要参数");
            }
            int i;
            try {
                i = commonAddressesService.backUpdateCommonAddresses(commonAddresses);
            } catch (Exception e){
                return AjaxResult.error(e.getMessage());
            }
            if (i > 0){
                return AjaxResult.success("修改成功");
            }
            else {
                return AjaxResult.error("修改失败");
            }
        }
        else {
            return AjaxResult.error("缺少必要参数");
        }
    }

    /**
     * 后端——删除常用地址
     * */
    @PostMapping("/backDeleteAddresses")
    public AjaxResult backDeleteCommonAddresses(@RequestBody Map<String, Object> map){
        if (map.get("commonAddresserId") != null && !map.get("commonAddresserId").equals("")){
            Long commonAddresserId = Long.valueOf(map.get("commonAddresserId").toString());
            int i;
            try {
                i = commonAddressesService.backDeleteCommonAddresses(commonAddresserId);
            } catch (Exception e){
                return AjaxResult.error(e.getMessage());
            }
            if (i > 0){
                return AjaxResult.success("删除成功");
            }
            else {
                return AjaxResult.error("删除失败");
            }
        }
        else {
            return AjaxResult.error("缺少必要参数");
        }
    }
}
